<?php
/**
 * Created by PhpStorm.
 * User: mr.hao
 * Date: 2018/7/23
 * Time: 14:41
 */

namespace Modules\Shop\service;

use Blade;
use Modules\Shop\Entities\Article;
use Modules\Shop\Entities\Category;
use Modules\Slider\Entities\Slider;

class Tagservice
{

//    因为make()方法要一次性加载多个组件，所以我们可以通过在他里边再调用方法让他一次性完成加载多个组件
    public function make(){
//        echo 111;
//        首先要创建分类标签,目的是为了不要过多的使用compact分配变量，直接就把数据带过去
        $this->fenlei();

        $this->wenzhang();

        $this->lunbo();

//        调用所有分类
        $this->suo();

    }


//    创建标签，使用blade方法
    public function fenlei(){
//        现获得所有的分类数据，把参数带过来,判断它有没有传参数，如果传了参数就进行下一步

//        获得所有的分类数据后，下一步，我不可能让所有的二级数据也显示在顶部，所以我要传参数
        Blade::directive('fenlei',function ($aidi){
        $aidi = $aidi?$aidi:'[]';
//        dd($aidi);
//            回调函数，意思就是你定义的标签是显示什么内容，就在回调函数里写射门内容
            $php = <<<php
<?php

\$parames = $aidi;
//获得所有的分类数据
\$db = new \Modules\Shop\Entities\Category();
if(isset(\$parames['pid'])){
    \$db = \$db->where('pid',\$parames['pid']);
}
\$data = \$db->get();
foreach(\$data as \$v){
    \$v['url'] = '/shop/lists/'.\$v['id']
?>
php;

        return $php;

        });

        Blade::directive('endfenlei',function (){
           $php  =  <<<php
<?php
}
?>
php;
        return $php;

        });
    }




//    自定义文章组件，把数据分配过去
    public function wenzhang()
    {

        Blade::directive('wenzhang', function ($canshu) {
            $canshu = $canshu ? $canshu : '[]';
            $php = <<<php
            <?php
                \$params = $canshu;
                \$dd =new \Modules\Shop\Entities\Article();
                //判断取哪个分类下的新闻
                if(isset(\$params['cid'])){
                    \$dd = \$dd->where('category_id',\$params['cid']);
                }
                //如果存在iscommend的值，就取推荐文章
                if(isset(\$params['iscommend'])){
                    \$dd = \$dd->where('iscommend',\$params['iscommend']);
                }
                //判断存不存在hot值，如果存在，就按照点击数排序
                if(isset(\$params['hot'])){
                    \$dd =\$dd->orderBy('click','desc');
                }
                //判断存不存在lmit值，如果不存在就取五条
                if(isset(\$params['limit'])){
                    \$dd = \$dd->limit(\$parama['limit']);
                }else{
                    \$dd = \$dd->limit(5);
                }
                \$wen = \$dd->get();
                
                foreach(\$wen as \$a){
                
                \$a['url']='/shop/contents/'.\$a['id'];
                
            ?>
php;
            return $php;
        });


        Blade::directive('endwenzhang',function (){
           $php = <<<php
            
            <?php
                }
            ?>
php;
        return $php;
        });
    }

//    创建轮播图组件
    public function lunbo(){

        Blade::directive('slider',function (){

            $php = <<<php
            

               
                  <div class="swiper-container">
                    <div class="swiper-wrapper">
                    
            <?php
                //获得轮播图
                \$slider = \Modules\Slider\Entities\Slider::where('is_show',1)->get();
                foreach(\$slider as \$v){
            ?>
                        <div class="swiper-slide"><img style="width: 100%;height: 100%" src="{{\$v['thumb']}}" alt=""></div>
                       
             <?php
                }
             ?>           
                        
                    </div>
                    <!-- 如果需要分页器 -->
                    <div class="swiper-pagination"></div>

                    <!-- 如果需要导航按钮 -->
                    <div class="swiper-button-prev"></div>
                    <div class="swiper-button-next"></div>

                </div>
          
php;

            return $php;

        });

    }

//    获得所有分类
    public function suo(){

        Blade::directive('suo',function (){

            $php  = <<<php

            <?php
                \$dc = new \Modules\Shop\Entities\Category();
                \$dv = \$dc->get();
                foreach(\$dv as \$c){
                    
            ?>
php;
        return $php;
        });


        Blade::directive('endsuo',function (){
            $php = <<<php

            <?php
                }
            ?>
php;
        return $php;
        });

    }



}